﻿using Microsoft.EntityFrameworkCore.Migrations;

#nullable disable

namespace BW.WMS.Migrations
{
    /// <inheritdoc />
    public partial class ConvertOrderFormTypeToInt : Migration
    {
        /// <inheritdoc />
        protected override void Up(MigrationBuilder migrationBuilder)
        {
            // 步骤1: 添加临时的整数列
            migrationBuilder.AddColumn<int>(
                name: "TypeTemp",
                table: "AppOrderFormModel",
                type: "int",
                nullable: false,
                defaultValue: 1);

            // 步骤2: 转换现有数据
            // 将字符串值转换为对应的整数值
            migrationBuilder.Sql(@"
                UPDATE AppOrderFormModel 
                SET TypeTemp = CASE 
                    WHEN Type = '入库' OR Type = '1' OR Type = 1 THEN 1
                    WHEN Type = '出库' OR Type = '2' OR Type = 2 THEN 2
                    ELSE 1 
                END");

            // 步骤3: 删除原来的字符串列
            migrationBuilder.DropColumn(
                name: "Type",
                table: "AppOrderFormModel");

            // 步骤4: 将临时列重命名为原列名
            migrationBuilder.RenameColumn(
                name: "TypeTemp",
                table: "AppOrderFormModel",
                newName: "Type");
        }

        /// <inheritdoc />
        protected override void Down(MigrationBuilder migrationBuilder)
        {
            // 回滚操作：将int转换回string
            
            // 步骤1: 添加临时的字符串列
            migrationBuilder.AddColumn<string>(
                name: "TypeTemp",
                table: "AppOrderFormModel",
                type: "longtext",
                nullable: false,
                defaultValue: "入库")
                .Annotation("MySql:CharSet", "utf8mb4");

            // 步骤2: 转换数据
            migrationBuilder.Sql(@"
                UPDATE AppOrderFormModel 
                SET TypeTemp = CASE 
                    WHEN Type = 1 THEN '入库'
                    WHEN Type = 2 THEN '出库'
                    ELSE '入库' 
                END");

            // 步骤3: 删除整数列
            migrationBuilder.DropColumn(
                name: "Type",
                table: "AppOrderFormModel");

            // 步骤4: 重命名临时列
            migrationBuilder.RenameColumn(
                name: "TypeTemp",
                table: "AppOrderFormModel",
                newName: "Type");
        }
    }
}
